package log

import (
	"fmt"
	"github.com/fatih/color"
)

type consoleWriter struct {
	colorful bool
	d        *color.Color
	i        *color.Color
	w        *color.Color
	e        *color.Color
}

func createConsoleWriter(colorful bool) *consoleWriter {
	w := &consoleWriter{
		colorful: colorful,
	}

	if w.colorful {
		color.NoColor = false

		w.d = color.New(color.FgHiWhite)
		w.i = color.New(color.FgHiGreen)
		w.w = color.New(color.FgHiYellow)
		w.e = color.New(color.FgHiRed)
	}

	return w
}

func (w *consoleWriter) Write(record IRecord) {
	s := string(record.Bytes())

	if !w.colorful {
		fmt.Print(s)
		return
	}

	switch record.Level() {
	case LevelDebug:
		w.d.Print(s)
	case LevelInfo:
		w.i.Print(s)
	case LevelWarn:
		w.w.Print(s)
	case LevelError:
		w.e.Print(s)
	}
}
